!function(e){"use strict";let t=!1;function s(e){e.fadeTo(100,0,function(){e.slideUp(100,function(){e.remove()})})}function i(e,t,s){var i=(t=t.replace("[]","[%%index%%]")).match(/([^\[\]])+/g);if(i)for(var r=i.length,o=e,c=0;c<r;c++){var l=String(i[c]);c==r-1?"%%index%%"===l?o.push(s):o[l]=s:("%%index%%"===i[c+1]?n(o[l])||(o[l]=[]):a(o[l])||(o[l]={}),o=o[l])}}function n(e){return Array.isArray(e)}function a(e){return"object"==typeof e}e.fn.getCursorPosition=function(){var e=this.get(0);if(e){if("selectionStart"in e)return e.selectionStart;if(document.selection){e.focus();var t=document.selection.createRange(),s=document.selection.createRange().text.length;return t.moveStart("character",-e.value.length),t.text.length-s}}},e.fn.createSeoVarsList=function(t){let s='<ul class="wpc-seo-vars-list">';return Object.keys(t).length>0?e.each(t,function(e,t){s+='<li data-seovar="{'+e+'}">'+t+"</li>"}):s+="<li>"+wpcSeoVars.noSeoVarsMsg+"</li>",s+="</ul>",e(this).replaceWith(s),!0},e(document).mouseup(function(t){let s=e(".wpc-vars-container");s.length>0&&!s.is(t.target)&&0===s.has(t.target).length&&s.hide()}),e(document).ready(function(){e("form#post").on("submit",function(n){if(s(e(".wpc-field-notice")),s(e("#message")),!t){var a;n.preventDefault();let r,o;a=e(this),r=e("#publishing-action .spinner"),o={},r.addClass("is-active"),o.validateData=function e(t){for(var s={},n=t.find("select, textarea, input").serializeArray(),a=0;a<n.length;a++)i(s,n[a].name,n[a].value);return s}(a),wp.ajax.post("wpc-validate-seo-rules",o).always(function(){r.removeClass("is-active")}).done(function(e){t=!0,a.submit()}).fail(function(t){var s;let i=[],n,a,r;void 0!==t.errors&&(e.each(t.errors,function(e,t){i.push(t.message)}),i.length<1&&i.push("Error: Set was not saved."),s=i,n=e("form#post"),a="",e.each(s,function(e,t){a+="<p>"+t+"</p>"}),r='<div id="message" class="wpc-error notice notice-error is-dismissible">'+a+'<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>',void 0!==n&&(e("#message").length>0&&e("#message").remove(),n.before(r)))})}}),e(".wpc-seo-vars-list").createSeoVarsList(wpcSeoVars.seovars),e("body").on("click",".wpc-open-container",function(t){t.preventDefault();let s=e(this).data("field"),i=e("#wpc-vars-container-"+s);e("#wpc_seo_rules-"+s).focus(),i.toggle()}),e("body").on("focus keypress blur",".wpc-vars-insertable",function(t){let s=e(this).getCursorPosition();e(this).data("caret",s)}),e("body").on("click",".wpc-seo-vars-list li",function(){let t=e(this).data("seovar"),s=e(this).parents(".wpc-vars-container");if(void 0!==t){let i=e("#wpc_seo_rules-"+s.data("container")),n=i.data("caret");""===n&&(n=i.val().length),0===n?t+=" ":t=n===i.val().length?" "+t:" "+t+" ",insertAtCaret(i,t,n)}s.hide()}),e("body").on("change","#wpc_seo_rules-rule_post_type",function(){s(e(".wpc-field-notice"));let t=e(this).val(),i=e(".wpc-intersection-fields-wrapper").children(".spinner");i.addClass("is-active");let n={};n._wpnonce=e("#wpc_seo_rule_nonce").val(),n.postType=t,n.postId=e("#post_ID").val(),wp.ajax.post("wpc-get-indexed-filters",n).always(function(){i.removeClass("is-active")}).done(function(t){e("#wpc-intersections-table").replaceWith(t.html),e(".wpc-seo-vars-list").createSeoVarsList(t.seovars)}).fail(function(t){if(void 0!==t){var s;let i;s=t.message,void 0!==(i=e("#wpc-intersection-fields-container"))&&i.before('<div class="wpc-field-notice wpc-field-notice-error"><p>'+s+"</p></div>")}})})})}(jQuery);function insertAtCaret(e,t,s){let i=e.val(),n=i.substring(0,s)+t+i.substring(s);return n=n.replace(/ +(?= )/g,""),e.val(n),!0}